package com.koron.css2.clientCenter.impl;

import com.koron.common.bean.query.SeniorQueryBean;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.PrefStrategyBean;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.PrefStrategyMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class UserInfoListCount implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		MessageBean<HashMap> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", HashMap.class);
		HashMap<String,String> map = new HashMap<String,String>();
		 UserInfoQueryBean uiqBean = JsonUtils.objectToPojo(req.getData(), UserInfoQueryBean.class);
         //判断综合查询list是否存在
         if (uiqBean.getList() != null && uiqBean.getList().size() > 0) {
				/*
				for(int i=0;i<uiqBean.getList().size();i++) {
					if(uiqBean.getList().get(i).getFieldName().equals("stopMonths")) {
						Integer stopMonths =Integer.valueOf(uiqBean.getList().get(i).getQueryValue());
						String stopMonthStr = LocalDate.now().minusMonths(stopMonths).toString();
						uiqBean.setStopMonthStr(stopMonthStr);
						uiqBean.getList().remove(i);
					}
				}*/
             Iterator<SeniorQueryBean> it = uiqBean.getList().iterator();
             while (it.hasNext()) {
                 SeniorQueryBean itBean = it.next();
                 if (itBean.getFieldName().equals("stopMonths")) {
                     Integer stopMonths = Integer.valueOf(itBean.getQueryValue());
                     String stopMonthStr = LocalDate.now().minusMonths(stopMonths).toString();
                     uiqBean.setStopMonthStr(stopMonthStr);
                     it.remove();
                 }
             }
         }
			/*
			for(SeniorQueryBean queryList:uiqBean.getList()) {//将停用月份特殊化处理
				if(queryList.getFieldName().equals("stopMonths")) {
					Integer stopMonths =Integer.valueOf(queryList.getQueryValue());
					String stopMonthStr = LocalDate.now().minusMonths(stopMonths).toString();
					uiqBean.setStopMonthStr(stopMonthStr);
					uiqBean.getList().remove(queryList);
				}
			}*/
         if (uiqBean.getList() != null && uiqBean.getList().size() > 0) {
             uiqBean.setList(UserInfoList.concatSql(uiqBean.getList()));
         }
         if (uiqBean.getUseWaterType() != null && uiqBean.getUseWaterType().length > 0) {
             uiqBean.setWaterTypeId(uiqBean.getUseWaterType()[uiqBean.getUseWaterType().length - 1]);
             uiqBean.setWaterTypeLev(uiqBean.getUseWaterType().length);
         }
         UserInfoMapper uiMapper = factory.getMapper(UserInfoMapper.class);
//			Integer count = 0;
//
//			if (StringUtils.isBlank(uiqBean.getFuzzyQuery()) && (uiqBean.getList() == null || uiqBean.getList().size()==0)){
//				count = uiMapper.userInfoCount(uiqBean);
//			}else {
//				PageHelper.startPage(uiqBean.getPage(), uiqBean.getPageCount());
//			}
         // 获取客户列表
         //当模糊查询的条件为银行编号时单独处理
         if (uiqBean.getQueryType() == 6 && !StringUtils.isBlank(uiqBean.getFuzzyQuery())) {
             //先去accountInfo表查找
             AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
             AccountInfoBean bean = accountInfoMapper.getAccountByBankNo(uiqBean.getFuzzyQuery());
             //再去调用用户接口
             uiqBean.setQueryType(3);
             uiqBean.setFuzzyQuery(bean.getAccountNo());
         }
         //当模糊查询的条件为优惠策略时单独处理
         if (uiqBean.getQueryType() == 9 && !StringUtils.isBlank(uiqBean.getFuzzyQuery())) {
             PrefStrategyMapper mapper = factory.getMapper(PrefStrategyMapper.class);
             List<PrefStrategyBean> bean = mapper.selectPrefStrategysByname(uiqBean.getFuzzyQuery());
             if (bean != null && bean.size() > 0) {
                 uiqBean.setPrefStrategyBeans(bean);
             } else {
            	 map.put("count","0");
                 info.setData(map);
                 return info;
             }
         }
         //营业区域单独处理
         if (uiqBean.getBusinessArea() != null && !"".equals(uiqBean.getBusinessArea())) {
             BusinessAreaMapper mapper = factory.getMapper(BusinessAreaMapper.class);
             List<String> areaIdList = UserInfoList.queryChildrenArea(uiqBean.getBusinessArea(), userInfo.getCurWaterCode(), mapper);
             uiqBean.setBusinessAreaList(areaIdList);
         }
         //只获取总数。
         uiqBean.setSortFlag(2);
         uiMapper.userInfoList(uiqBean);
         int count = uiMapper.selectCount();
         map.put("count",""+count);
         
         info.setData(map);
		return info;
	}
}
